﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform.Dc
{
    /// <summary>
    /// 药品字典服务
    /// </summary>
    public class DrugBasInfoService : DcAppService
    {
        private readonly IRepository<DrugBasInfo, Guid> _drugBasInfoRepository;

        public DrugBasInfoService(IRepository<DrugBasInfo, Guid> drugBasInfoRepository)
        {
            _drugBasInfoRepository = drugBasInfoRepository;
        }

        /// <summary>
        /// 获取药品基本信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<DrugBasInfoDto>> GetListAsync(DrugBasInfoInput input)
        {
            var iQuery = await _drugBasInfoRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.PinyinCode), d => d.Pinyincode.Contains(input.PinyinCode) || d.DrugName.Contains(input.PinyinCode));
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.DrugCode), d => d.DrugCode.Contains(input.DrugCode) || d.DrugName.Contains(input.DrugCode));
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.DrugName), d => d.DrugName.Contains(input.DrugName) || d.DrugName.Contains(input.DrugName));

            if (input.SkipCount == 0)
            {
                var itemsDto = ObjectMapper.Map<List<DrugBasInfo>, List<DrugBasInfoDto>>(iQuery.ToList());
                return new PagedResultDto<DrugBasInfoDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var items = iQuery.Page(input.SkipCount, input.MaxResultCount);
                var itemsDto = ObjectMapper.Map<List<DrugBasInfo>, List<DrugBasInfoDto>>(items.ToList());
                return new PagedResultDto<DrugBasInfoDto>(itemsDto.Count, itemsDto);
            }
        }

        /// <summary>
        /// 创建药品基本信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<DrugBasInfoDto> PostAsync(DrugBasInfoCreateDto input)
        {
            DrugBasInfo drugBasInfo = new DrugBasInfo(input.DrugTypeId, input.PhamTypeId, input.DefChrgitmTypeId
                , input.DrugCode, input.DrugName, input.DrugDosformId, input.DefDrugMedcWayDictId
                , input.DefUsedFrquDictId);
            drugBasInfo = await _drugBasInfoRepository.InsertAsync(drugBasInfo);
            var drugBaseInfoDto = ObjectMapper.Map<DrugBasInfo, DrugBasInfoDto>(drugBasInfo);

            return drugBaseInfoDto;
        }

        /// <summary>
        /// 更新药品基本信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<DrugBasInfoDto> PutAsync(Guid id, DrugBasInfoCreateDto input)
        {
            var drugBasInfo = await _drugBasInfoRepository.GetAsync(id);
            drugBasInfo = ObjectMapper.Map(input, drugBasInfo);
            drugBasInfo = await _drugBasInfoRepository.UpdateAsync(drugBasInfo);
            var drugBaseInfoDto = ObjectMapper.Map<DrugBasInfo, DrugBasInfoDto>(drugBasInfo);

            return drugBaseInfoDto;
        }

        /// <summary>
        /// 删除药品基本信息
        /// </summary>
        /// <returns></returns>
        public async Task DeleteAsync(Guid id)
        {
            await _drugBasInfoRepository.DeleteAsync(id);
        }
    }
}
